home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / visulztn / saoimage / saoimage.lha / rgnread.c < prev    next >
C/C++ Source or Header  |  1991-01-08  |  8KB  |  260 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    rgnread.c (Region Read)
  6.  * Purpose:    Open and read ASCII region specification files and cursor lists
  7.  * Subroutine:    read_regions()        returns: void
  8.  * Xlib calls:    none
  9.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  10.  *        You may do anything you like with this file except remove
  11.  *        this copyright.  The Smithsonian Astrophysical Observatory
  12.  *        makes no representations about the suitability of this
  13.  *        software for any purpose.  It is provided "as is" without
  14.  *        express or implied warranty.
  15.  * Modified:    {0} Michael VanHilst    initial version        9 August 1989
  16.  *              {1} MVH BSDonly strings.h compatability           19 Feb 1990
  17.  *        {n} <who> -- <does what> -- <when>
  18.  */
  19.  
  20. #include <stdio.h>        /* stderr, NULL, etc. */
  21. #include <ctype.h>        /* toupper, isalpha, etc. */
  22.  
  23. #ifndef VMS
  24. #ifdef SYSV
  25. #include <string.h>        /* strlen, strcat, strcpy, strrchr */
  26. #else
  27. #include <strings.h>        /* strlen, strcat, strcpy, rindex */
  28. #endif
  29. #else
  30. #include <string.h>        /* strlen, strcat, strcpy, strrchr */
  31. #endif
  32.  
  33. #include <X11/Xlib.h>        /* X window stuff */
  34. #include <X11/Xutil.h>        /* X window manager stuff */
  35. #include "hfiles/constant.h"    /* define codes */
  36. #include "hfiles/define.h"    /* SZ_FNAME, SZ_LINE */
  37. #include "hfiles/struct.h"    /* declare structure types */
  38. #include "hfiles/extern.h"    /* extern main SAOimage parameter structures */
  39. #include "hfiles/edit.h"    /* EditStruct */
  40. #include "hfiles/region.h"    /* region parsing record (reg_param) */
  41.  
  42. extern EditStruct *region_edit;    /* key to popup editor for file name input */
  43.  
  44.  
  45. #ifdef ANSIC
  46.  
  47. void        read_regions();
  48. static int  check_special(    char *line);
  49. static int  determine_file_type(char *line, int len);
  50. static void parse_imtool_points(char *line);
  51. static void parse_pros_regions ( char *line )
  52. static void parse_point(     char *line, int exclude);
  53.  
  54. #else
  55.  
  56. static int check_special();
  57. static int determine_file_type();
  58. static void parse_imtool_points(), parse_pros_regions(), parse_point();
  59.   EditStruct *init_edit_popup();
  60.   int open_input_file();
  61.   void disp_dispbox();
  62.   int check_parens(), burst_line();
  63.   void make_next_region(), fit_annuli_edge(), new_annulus_edge();
  64.   struct cursorRec *get_new_cursor();
  65.   void set_cursor_from_file_coords(), free_cursor(), disp_region();
  66.  
  67. #endif
  68.  
  69.  
  70. /*
  71.  * Subroutine:    read_regions
  72.  * Purpose:    Read cursors into the save cursor records from a file
  73.  * Note:    Each new entry is added at base of list
  74.  */
  75. void read_regions ( )
  76. {
  77.   FILE *fd;
  78.   int type;
  79.   char line[SZ_LINE];
  80.  
  81.   if( region_edit == NULL )
  82.     region_edit = init_edit_popup((char *)NULL, SZ_FNAME);
  83.   /*  Open coordinate output file for writing  */
  84.   if( open_input_file(&fd, region_edit, 0,
  85.               "Enter file name of IRAF list or PROS regions:") <= 0 )
  86.     return;
  87.   while( (fgets(line, SZ_LINE, fd) != NULL) &&
  88.      ((type = determine_file_type(line, SZ_LINE)) == 0) );
  89.   if( type == SOP_Imtool ) {
  90.     do {
  91.       /*  Parse the line, allocating region records  */
  92.       parse_imtool_points (line);
  93.     } while( fgets(line, SZ_LINE, fd) != NULL );
  94.   } else if( type == SOP_PROS ) {
  95.     do {
  96.       /*  Parse the line, allocating region records  */
  97.       parse_pros_regions (line);
  98.     } while( fgets(line, SZ_LINE, fd) != NULL );
  99.   }
  100.   /*  Close the region file  */
  101.   (void)fclose(fd);
  102.   /*  Draw the display with regions labeled as per display selection  */
  103.   (void)disp_dispbox();
  104. }
  105.  
  106.  
  107. /*  Subroutine:    determine_file_type
  108.  *  Purpose:    Determine if line is commented or blank
  109.  *  Returns:    0 if line is blank fo comment, SOP_Imtool if starts with a
  110.  *        number, SOP_PROS if starts with a letter, +, or -
  111.  */
  112. #ifdef ANSIC
  113. static int determine_file_type ( char *line, int len )
  114. #else
  115. static int determine_file_type ( line, len )
  116.      char *line;
  117.      int len;
  118. #endif
  119. {
  120.   int i;
  121.   if( check_special(line) )
  122.     return( SOP_PROS );
  123.   for( i=0; i<len; i++ ) {
  124.     if( (line[i] == '\0') || (line[i] == '#') || (line[i] == '\n') )
  125.       return( 0 );
  126.     if( isascii(line[i]) && (!isspace(line[i])) ) {
  127.       if( isdigit(line[i]) )
  128.     return( SOP_Imtool );
  129.       else if( isalpha(line[i]) || (line[i] == '!') ||
  130.            (line[i] == '-') || (line[i] == '+') )
  131.     return( SOP_PROS );
  132.     }
  133.   }
  134.   return( 0 );
  135. }
  136.  
  137.  
  138. /*  Subroutine:    parse_imtool_points
  139.  *  Purpose:    Read region info in imtool's format
  140.  */
  141. #ifdef ANSIC
  142. static void parse_imtool_points ( char *line )
  143. #else
  144. static void parse_imtool_points ( line )
  145.      char *line;
  146. #endif
  147. {
  148.   /*  Ignore blank or commented lines  */
  149.   if( (line[0] != '\0') && (line[0] != '#') && (line[0] != '\n') )
  150.     /*  Parse line as single point for exclusion (IRAF bad pixel list)  */
  151.     parse_point(line, 1);
  152. }
  153.  
  154.  
  155. /*  Subroutine:    parse_pros_regions
  156.  *  Purpose:    Read region info in PROS format
  157.  */
  158. #ifdef ANSIC
  159. static void parse_pros_regions ( char *line )
  160. #else
  161. static void parse_pros_regions ( line )
  162.      char *line;
  163. #endif
  164. {
  165.   int count, len, i;
  166.   int scratch_sz = 128;
  167.   float scratch[128];
  168.   struct reg_param records[REG_LIMIT];
  169.   char temp[SZ_LINE];
  170.  
  171.  
  172.   /*  If we get a special label type, advance beyond the protective #  */
  173.   if( check_special(line) )
  174.     (void)strcpy(temp, &line[1]);
  175.   else
  176.     (void)strcpy(temp, line);
  177.   /*  Check parens and set all token separators to ascii space  */
  178.   if( (len = check_parens(line, temp)) < 5 )
  179.     return;
  180.   /*  Identify and count distinct region specifications  */
  181.   if( (count = burst_line(line, temp, len, records)) < 1 )
  182.     return;
  183.   /*  If 2 regions forming an annulus  */
  184.   if( (count == 2) && (records[0].not == 0) && (records[1].not == 1) ) {
  185.     make_next_region(records[1].type, records[1].line,
  186.              records[1].exclude, 0, scratch, scratch_sz);
  187.     /*  If the inner edge is same as previous edge  */
  188.     if( cursor.next_region->next_region != NULL )
  189.       fit_annuli_edge(&cursor);
  190.     make_next_region(records[0].type, records[0].line,
  191.              records[0].exclude, 1, scratch, scratch_sz);
  192.     new_annulus_edge(&cursor);
  193.   } else {
  194.     for( i=0; i<count; i++ )
  195.       make_next_region(records[i].type, records[i].line,
  196.                records[i].exclude, 1, scratch, scratch_sz);
  197.   }
  198. }
  199.  
  200.  
  201. /*  Subroutine:    parse_point
  202.  *  Purpose:    parse a point's parameters
  203.  *  Note:    Recurse on multiple center parameters, to make additional
  204.  *        point regions
  205.  */
  206. #ifdef ANSIC
  207. static void parse_point ( char *line, int exclude )
  208. #else
  209. static void parse_point ( line, exclude )
  210.      char *line;        /* line to parse for center */
  211.      int exclude;        /* include or exclude point(s) */
  212. #endif
  213. {
  214.   struct cursorRec *region;
  215.  
  216.   /*  Get cleared record space and set some key parameters  */
  217.   region = get_new_cursor(0, 0);
  218.   region->type = COP_Point;
  219.   region->exclude_region = exclude;
  220.   if( sscanf(line, "%f%*c%f", ®ion->file.X, ®ion->file.Y) != 2 ) {
  221.     free_cursor(region);
  222.   } else {
  223.     /*  Set up this point region  */
  224.     set_cursor_from_file_coords(region, &coord.filetodisp);
  225.     if( (region->next_region = cursor.next_region) != NULL )
  226.       region->index = cursor.next_region->index + 1;
  227.     else
  228.       region->index = 1;
  229.     cursor.next_region = region;
  230.     if( (region->exclude_region = exclude) )
  231.       region->draw = &color.gcset.excl;
  232.     else
  233.       region->draw = &color.gcset.incl;
  234.     region->win.display = dispbox.display;
  235.     region->win.ID = dispbox.ID;
  236.     region->overwrites_image_data = 1;
  237.     disp_region(region);
  238.   }
  239. }
  240.  
  241.  
  242. /*  Subroutine:    check_special
  243.  *  Purpose:    Look for special label type cursor declaration
  244.  */
  245. #ifdef ANSIC
  246. static int check_special( char *line )
  247. #else
  248. static int check_special( line )
  249.      char *line;
  250. #endif
  251. {
  252.   if(   (strncmp(line, "# ARROW(", 8) == 0)
  253.      || (strncmp(line, "#-ARROW(", 8) == 0)
  254.      || (strncmp(line, "# TEXT(", 7) == 0)
  255.      || (strncmp(line, "#-TEXT(", 7) == 0) )
  256.     return( 1 );
  257.   else
  258.     return( 0 );
  259. }
  260.